/**
 * 统计页面设备js
 * @author lh
 * 2016-01-15
 *
 */
/**
 * 设备数据表格属性配置
 */
//设置表格数据的样式。高度和宽度，刷新页面，样式重新调整
$('#medicalEquipment_eCharts').css('height',orange.mainPage.getHeight()-15);
$('#medicalEquipment_eCharts').css('width',orange.mainPage.getWidth());
function me_query(){
	$('#medicalEquipment_Grid').datagrid({
		url:'s/deviceRes/table',//+'/'+encodeURI(encodeURI("浙江省"))+'/'+encodeURI(encodeURI("杭州市"))+'/'+encodeURI(encodeURI("上城区")),
		queryParams:{
			province:$('#me_province').combobox('getText')=='全国'?'':$('#me_province').combobox('getText'),//'北京市',
			city:$('#me_city').combobox('getText')=='全省'?'':$('#me_city').combobox('getText'),//"北京市",
			dist:$('#me_district').combobox('getText')=='全市'?'':$('#me_district').combobox('getText')//"昌平区"
		},
		loadFilter:orange.systemPage.selfLoadFilter
	});
	me_reLoadOption($("#dic_Equipment").combobox('getValue'));
}
	$('#medicalEquipment_Grid').datagrid({ 
		method:'post',
		url:'s/deviceRes/table',//+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("东城区")),
		queryParams:{
			province:'',
			city:"",
			dist:""
		},
		loadFilter:orange.systemPage.selfLoadFilter,
	    singleSelect:true,
	    rownumbers:true,
	    columns:[[ 
			{field:'guid',hidden:true,title:'设备id'},
			{field:'orgGuid',hidden:true,title:'机构id'},
			{field:'orgName',title:'中医馆名',width:'10%',sortable:true,halign:'center',formatter:me_checkDetail},
	        {field:'c1',title:'B超',width:'10%',sortable:true,halign:'center',formatter:option}, 
	        {field:'c2',title:'生化仪',width:'10%',sortable:true,halign:'center',formatter:option},
	        {field:'c3',title:'血液分析',width:'10%',sortable:true,halign:'center',formatter:option},
	        {field:'c4',title:'CT',width:'10%',sortable:true,halign:'center',formatter:option},
	        {field:'c5',title:'面诊仪',width:'10%',sortable:true,halign:'center',formatter:option}, 
	        {field:'c6',title:'脉象仪',width:'10%',sortable:true,halign:'center',formatter:option}, 
	        {field:'c7',title:'理疗仪',width:'10%',sortable:true,halign:'center',formatter:option}, 
	        {field:'c8',title:'体质辨识',width:'10%',sortable:true,halign:'center',formatter:option}, 
	        {field:'c9',title:'其他',width:'8%',sortable:true,halign:'center',formatter:option}
	    ]],
	    pagination:true,
	    sortable:true,
	    sortName: 'type',
	    sortOrder: 'asc',
	    onLoadSuccess:function(data){
//	    	debugger;
	    }
	});
	function option(value){
		if(value!=null){
			return value;
		}else{
			return 0;
		}
	}
	//grid link
	//选中的中医馆id,第一次加载该页面是未定义,调用switch isExist的方法，加判断
	function me_checkDetail(value, row, index){
		if(value==null){
			return null;
		}else{
			return '<a href="#" onclick="tcmReloadOrCreate(\''+row.guid+'\',\''+row.orgName+'\')">'+value+'</a>';	
		}
	};
	var clearData=[];
	//combobox设备的字典表
	var dicData=[{'id':1,'text':'B超'},{'id':2,'text':'生化仪'},{'id':3,'text':'血液分析'},{'id':4,'text':'CT'}
	,{'id':5,'text':'面诊仪'},{'id':6,'text':'脉象仪'}];
	$("#dic_Equipment").combobox({
//		url:'/statistics/device',
		data:dicData,   
	    valueField:'id',   
	    textField:'text',
	    panelHeight:'auto',
		width:'150px',
	    onLoadSuccess:function(data){
	    	$("#dic_Equipment").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	me_reLoadOption(node.id);
	    }
	});
	//查询条件combobox配置
//	$('#show_me_city')[0].style.display="none";
//	$('#show_me_district')[0].style.display="none";
	//省级数据
	var pData=[{'id':1,'text':'全国'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'},{'id':4,'text':'河北省'}];
	$("#me_province").combobox({
		data:pData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
	    	$("#me_province").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	if(node.id==1){
	    		$("#me_city").combobox({data:clearData});
	    		$("#me_district").combobox({data:clearData});
//	    		$("#me_city + .combo").hide();
//	    		$("#me_district + .combo").hide();
//	    		$("#me_city").combobox('setValue','00');
//	    		$("#me_district").combobox('setValue','00');
	    	}else{
//	    		$('#show_me_city')[0].style.display="inline-block";
	    		me_city();//$("#me_city + .combo").show();
	    	}
	    }
	});
	var cData=[{'id':1,'text':'全省'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'}
	,{'id':4,'text':'石家庄市'}];
	$("#me_city").combobox({
		data:cData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
//	    	$("#me_city").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	if(node.id==1){
	    		$("#me_district").combobox({data:clearData});
//	    		$("#me_district + .combo").hide();
//	    		$("#me_district").combobox('setValue','00');
	    	}else{
//	    		$('#show_me_district')[0].style.display="inline-block";
	    		me_district();//$("#me_district + .combo").show();
	    	}
	    } 
	});
	var tData=[{'id':1,'text':'全市'},{'id':2,'text':'新华区'},{'id':3,'text':'朝阳区'}
	,{'id':4,'text':'西城区'}];
	$("#me_district").combobox({
		data:tData,   
	    valueField:'id',   
	    textField:'text' ,
	    onLoadSuccess:function(data){
//	    	$("#me_district").combobox('setValue',data[0].id);
	    }
	});
	function me_city(){
		debugger;
		//市级数据
		 $('#me_city').combobox({data:cData});    
	}
	
	function me_district(){
		//区域数据
		$('#me_district').combobox({data:tData});
	}
	function me_reLoadOption(id){
		$.ajax({
	        url:'s/chartDevice',//+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("东城区")),
	        type: 'post',
	        data:{
	        	type:id,//$("#dic_Equipment").combobox('getValue'),
	        	province:$('#me_province').combobox('getText')=='全国'?'':$('#me_province').combobox('getText'),//'北京市',
    			city:$('#me_city').combobox('getText')=='全省'?'':$('#me_city').combobox('getText'),//"北京市",
    			dist:$('#me_district').combobox('getText')=='全市'?'':$('#me_district').combobox('getText')//"昌平区"
	        },
	        success: function(data) {
	        	var initData=eval(data);
	        	var seriesData1=[];//纵轴数据
	        	var seriesData2=[];//纵轴数据
	        	var xData=[];//横轴数据
	        	var myChart = echarts.init($("#medicalEquipment_eCharts")[0]);
	        	for (var i=0;i<initData.length;i++){
	        		xData.push(initData[i].period);
	        		seriesData1.push(initData[i].deviceTotal);
	        		seriesData2.push(initData[i].deviceNum);
	        	}
	        	me_eChart_option(xData,seriesData1,seriesData2,myChart);
	        }
	    });
	}
	//隐藏市区划分
	//echarts图表配置
	//ajax请求，加载数据,页面初始化加载
	$.ajax({
        url:'s/chartDevice',//+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("东城区")),
        type: 'post',
        data:{
        	type:$("#dic_Equipment").combobox('getValue'),
        	province:'',
			city:"",
			dist:""},
        success: function(data) {
        	var initData=eval(data);
        	var seriesData1=[];//纵轴数据
        	var seriesData2=[];//纵轴数据
        	var xData=[];//横轴数据
        	var myChart = echarts.init($("#medicalEquipment_eCharts")[0]);
        	for (var i=0;i<initData.length;i++){
        		xData.push(initData[i].period);
        		seriesData1.push(initData[i].deviceTotal);
        		seriesData2.push(initData[i].deviceNum);
        	}
        	me_eChart_option(xData,seriesData1,seriesData2,myChart);
        }
    });
    // 指定图表的配置项和数据
	function me_eChart_option(xData,seriesData1,seriesData2,myChart){
		option = {
				grid:{
					show:false,
					bottom :'15%',
					top:'22%',
					height:'68%',
					width:'85%',
					left:'7%',
					right:'8%'
				},
			    tooltip : {
			        trigger: 'axis'
			    },
			    legend: {
			        data:['设备总量','该类设备总量'],
			        top:'5%'
			    },
			    calculable : true,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : false, 
			            data : xData//['2013-04','2013-07','2013-10','2014-01','2014-07','2014-10','2015-01','2015-04','2015-07','2015-10','2016-01']
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value'
			        }
			    ],
			    series : [
			        {
			            name:'设备总量',
			            type:'line',
			            data:seriesData1
			        },
			        {
			            name:'该类设备总量',
			            type:'line',
			            data:seriesData2
			        }
			    ]
			};
			// 使用刚指定的配置项和数据显示图表。
		    myChart.setOption(option);
	}
	
